package com.bj58.groupbuy.action.service.impl;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.TreeMap;

import com.bj58.groupbuy.action.db.CommonDao;
import com.bj58.groupbuy.action.db.DBConfig;
import com.bj58.groupbuy.action.model.OperMenu;
import com.bj58.groupbuy.action.model.RoleOperPriviledge;
import com.bj58.groupbuy.action.model.User;
import com.bj58.groupbuy.action.model.UserOperPriviledge;
import com.bj58.groupbuy.action.model.UserRole;
import com.bj58.groupbuy.action.service.UserOperService;
import com.bj58.groupbuy.action.util.MyStore;

public class UserOperServiceImpl implements UserOperService {
	private CommonDao comDao = new CommonDao();
	
	
	@Override
	public HashSet<Long> getUserOpers(long userId) throws Exception {
		//用户操作权限
		List<UserOperPriviledge> userOperList =  comDao.getAllColsByVo("select * from user_oper_priviledge where userid = " + userId, UserOperPriviledge.class, DBConfig.db_68_bi_sys.getIntType());
		HashSet<Long> opersSet = new HashSet<Long>();
		for (UserOperPriviledge vo : userOperList) {
			opersSet.add(vo.getOperid());
		}
		
		//用户角色
		List<UserRole> roleList =  comDao.getAllColsByVo("select * from user_role where userid = " + userId, UserRole.class, DBConfig.db_68_bi_sys.getIntType());
		if(!roleList.isEmpty()){
			StringBuffer rolestemp = new StringBuffer();
			for (UserRole userRole : roleList) {
				rolestemp.append(userRole.getRoleid() + ",");
			}
			String roles = rolestemp.toString().substring(0, rolestemp.toString().lastIndexOf(","));
			//用户角色操作权限
			List<RoleOperPriviledge> roleOperList = comDao.getAllColsByVo("select * from role_oper_priviledge where roleid in (" +  roles +") ", RoleOperPriviledge.class, DBConfig.db_68_bi_sys.getIntType());
			for (RoleOperPriviledge vo : roleOperList) {
				opersSet.add(vo.getOperid());
			}
		}
	
		return opersSet;
	}

	@Override
	public List<OperMenu> getAllOpers() throws Exception {
		return comDao.getAllColsByVo("select * from oper_menu where state = 1 ", OperMenu.class, DBConfig.db_68_bi_sys.getIntType());
	}

	@Override
	public TreeMap<String, List<OperMenu>> getAllOpersMap() throws Exception {
		List<OperMenu> operMenuList = getAllOpers();
		TreeMap<String, List<OperMenu>> operMenuMap = new TreeMap<String, List<OperMenu>>();
		for (OperMenu operMenu : operMenuList) {
			String key = operMenu.getCate1() + "-" + operMenu.getCate2();
			List<OperMenu> tempList = new ArrayList<OperMenu>();
			if (operMenuMap.containsKey(key)) {
				tempList = operMenuMap.get(key);
			}
			tempList.add(operMenu);
			operMenuMap.put(key, tempList);
		}
		return operMenuMap;
	}

	
	@Override
	public boolean inserUserOpers(List<UserOperPriviledge> operList)
			throws Exception {
		return comDao.insertVoList("user_oper_priviledge", UserOperPriviledge.class, operList, DBConfig.db_68_bi_sys.getIntType());
	}

	@Override
	public boolean deleteOpersByUser(long userid) throws Exception {
		return comDao.deleteData("user_oper_priviledge", " and userid = " + userid, DBConfig.db_68_bi_sys.getIntType());
	}

	@Override
	public boolean checkUserOper(User user, int operid) throws Exception {
		//权限点状态校验 
		int has = comDao.countAll("select count(1) from oper_menu where id="+operid+" and state= 1",  DBConfig.db_68_bi_sys.getIntType());
		if(has != 1){
			return false;
		}
		
		//权限校验
		if(MyStore.admin.equals(user.getFunction())){ //管理员权限直接通过
			return true;
		}
		
		int res = comDao.countAll("select count(1) from user_oper_priviledge where userid="+user.getId()+" and operid="+operid,  DBConfig.db_68_bi_sys.getIntType());
		return res == 1?true:false;
	}

	@Override
	public List<OperMenu> getOperListByCate(String cate1, String cate2)
			throws Exception {
		return comDao.getAllColsByVo("select * from oper_menu where cate1='" + cate1+"' and cate2='"+cate2+"' and state = 1", OperMenu.class, DBConfig.db_68_bi_sys.getIntType());
	}

	@Override
	public HashSet<Long> getRoleOpers(long roleid) throws Exception {
		List<RoleOperPriviledge> rsList =  comDao.getAllColsByVo("select * from role_oper_priviledge where roleid = " + roleid, RoleOperPriviledge.class, DBConfig.db_68_bi_sys.getIntType());
		HashSet<Long> opersSet = new HashSet<Long>();
		for (RoleOperPriviledge vo : rsList) {
			opersSet.add(vo.getOperid());
		}
		return opersSet;
	}

	
}
